﻿/// File: ViewletControls\label.fs
/// 
/// Author: Giacomo Righetti (righetti@di.unipi.it)
///   
/// ------------------------------------------------------------
/// Copyright (c) Microsoft Corporation.  All rights reserved.
/// 
/// The use and distribution terms for this software are 
/// contained in the file named license.txt, which can be found 
/// in the root of this distribution.
/// By using this software in any fashion, you are agreeing to 
/// be bound by the terms of this license.
///
/// You must not remove this notice, or any other, from this
/// software.
/// ------------------------------------------------------------

namespace LightweightControls

open System.Drawing
open System.Windows.Forms

type LightweightLabel() as x =
    inherit LightweightControl()
    
    let mutable text = ""
    let mutable foreColor = SystemColors.ControlText
    let mutable borderStyle = BorderStyle.None
    
    ///        
    /// Summary:
    ///     Gets or sets the text associated with this control.
    ///
    // Returns:
    ///     The text associated with this control.
    member x.Text
        with get() = text
        and set(t) = 
            text <- t
            x.Size <- TextRenderer.MeasureText(x.Text, x.Font)
            // NB: it is not necessary to call Invalidate bacause it is called by the set method of Size property.
    
    ///
    /// Summary:
    ///     Gets or sets the foreground color of the control.
    ///
    /// Returns:
    ///     The foreground System.Drawing.Color of the control. The default is the value
    ///     of the System.Windows.Forms.Control.DefaultForeColor property.
    member x.ForeColor
        with get() = foreColor
        and set(c) = 
            foreColor <- c
            base.Invalidate()
    
    ///
    /// Summary:
    ///     Indicates the border style for the control.
    ///
    /// Returns:
    ///     One of the System.Windows.Forms.BorderStyle enumeration values. 
    ///     The default is System.Windows.Forms.BorderStyle.None.
    member x.BorderStyle
        with get() = borderStyle
        and set(value) =
            if borderStyle <> value then 
                borderStyle <- value
                base.Invalidate()
        
    override x.OnPaint (e) =
        let g = e.Graphics
        
        // draws the border
        if borderStyle = BorderStyle.FixedSingle then
            ControlPaint.DrawBorder(g, x.ClientRectangle, SystemColors.ControlDarkDark, ButtonBorderStyle.Solid)
        elif borderStyle = BorderStyle.Fixed3D then
            ControlPaint.DrawBorder3D(g, x.ClientRectangle, Border3DStyle.Sunken, Border3DSide.All)
                        
        if x.Enabled then 
            use brush = new SolidBrush(foreColor)
            g.DrawString(x.Text, x.Font, brush, PointF.Empty)
        else 
            g.DrawString(x.Text, x.Font, SystemBrushes.GrayText, PointF.Empty)